package org.simtube.cluster;

import java.util.ArrayList;

import org.simtube.preprocess.FilePreprocess;
import org.simtube.util.*;

public class Clustering {
	
	private FilePreprocess filePreprocess;
	private DocItem[] dvectors;
	
	protected DocItem[] getDvectors() {
		return dvectors;
	}
	public void setDvectors(DocItem[] data){
		this.dvectors = data;
		this.dims = data[0].vector.getDim();
	}
	public FilePreprocess getFilePreprocess() {
		return filePreprocess;
	}
	public void setFilePreprocess(FilePreprocess filePreprocess) {
		this.filePreprocess = filePreprocess;
	}
	private int dims;
	
	protected int getDims() {
		return dims;
	}
	public Clustering(FilePreprocess f){
		this.filePreprocess = f;
	}
	/**
	 * constructor
	 * @param data  document vectors
	 */
	public Clustering(DocItem[] data, FilePreprocess f)
	{
		this.dvectors = data;
		this.dims = data[0].vector.getDim();
		filePreprocess = f;
	}
	
	/**
	 * constructor
	 * @param data  document vectors
	 * @param _dims   dimension of the vectors
	 */
	public Clustering(DocItem[] data, int _dims)
	{
		this.dvectors = data;
		this.dims = data[0].vector.getDim();
		if (_dims > 0) this.dims = _dims;
		else throw new IllegalArgumentException("dims <= 0");
	}
	
	/**
	 * Perform clustering on given array of doc id.
	 * @param docs  the array of doc id
	 * @param k   the number of clusters
	 * @return   A list, each element is an array of doc id in one cluster
	 */
	public ArrayList<Cluster> cluster(int[] docs, int k)
	{
		try
		{
			return (new KMeans(this)).cluster(docs, k);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Perform clustering on given array of doc id.
	 * The k will be determined automatically.
	 * @param docs  the array of doc id
	 * @return   A list, each element is an array of doc id in one cluster
	 */
	public ArrayList<Cluster> cluster(int[] docs)
	{
		try
		{
			return (new KMeans(this)).cluster(docs, -1);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
}
